<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="assignPerm(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(scope.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(scope.row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <!--
                🎈属性:
                current-page	当前页数，支持 .sync 修饰符	默认值 1
                page-sizes	每页显示个数选择器的选项设置 默认值[10, 20, 30, 40, 50, 100]
                total	总条目数
                🎈事件:
                current-change	currentPage 改变时会触发跳转  -->
              <el-pagination
                layout="prev,pager,next"
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="changepage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
      <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" :placeholder="roleForm1.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" :placeholder="roleForm1.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

    </div>

    <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
      <!-- 权限是一颗树 -->
      <!-- 将数据绑定到组件上 -->
      <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
      <!-- id作为唯一标识 -->
      <!-- :default-checked-keys="selectCheck" -->
      <!-- :props="{label:name}" -->
      <el-tree
        ref="permTree"
        :data="permList"
        :props="{label:'name'}"
        :show-checkbox="true"
        :check-strictly="true"
        :default-expand-all="true"

        node-key="id"
      />
      <!-- 确定 取消 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, deleteRole, getRole, updateRole, addRole, assignPerm } from '@/api/setting'
import { mapGetters } from 'vuex'
import { getPermissionList } from '@/api/permission'
import { listToTreeData } from '@/utils/index'

export default {
  data() {
    return {
      formData: {
        // 公司名字
        name: '',
        // 公司地址
        companyAddress: '',
        // 公司邮箱
        mailbox: '',
        // 公司口碑
        remarks: ''
      },

      page: {
        page: 1,
        pagesize: 2,
        total: 0

      },
      list: [],
      showDialog: false,
      // 员工弹窗数据
      roleForm: {
        name: '',
        description: '',
        id: ''
      },
      roleForm1: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '数据不能为空', tigger: 'blur' }
        ],
        description: [
          { required: true, message: '数据不能为空', tigger: 'blur' }
        ]
      },
      showPermDialog: false,
      roleId: '',
      permList: []
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    showTitle() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }

  },
  created() {
    this.getCompanyInfo()
    this.getRoleList()
  },
  methods: {
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
      // console.log(res)
    },
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      // console.log(res)
      this.page.total = total
      this.list = rows
    },
    changepage(newpage) {
      this.page.page = newpage
      this.getRoleList()
    },
    // 删除角色
    async deleteRole(id) {
      // 二次校验
      await this.$confirm('是否真的删除')
      // 发请求
      await deleteRole(id)
      // 担心当前页面删没了,怎么办
      // 1.如果当前页面只剩一条,应该向前翻页
      // 2.如果当前已经是第一页则是例外,不翻页
      if (this.list.length === 1 && this.page.page !== 1) {
        this.page.page--
      }
      // 提示成功
      this.$message.success('删除成功')
      // 刷新
      this.getRoleList()
    },
    // 编辑
    async editRole(id) {
      this.roleForm1 = await getRole(id)
      this.roleForm.id = this.roleForm1.id
      // console.log('111', this.roleForm1)
      this.showDialog = true
      // console.log('1222', this.roleForm1)
    },
    // 新增角色
    // addRole() {
    //   this.showDialog = true
    //   this.roleForm = {}
    // },
    async btnOK() {
      // 1.校验表单
      await this.$refs.roleForm.validate()
      // 2.发请求
      if (this.roleForm1.id) {
        // 编辑角色
        await updateRole(this.roleForm)
      } else {
        // 新增角色
        await addRole(this.roleForm)
        // 做完这里就是新增,由于不一定在最后一页
        // 所以要判断是否已满 不能用当前页面的数据
        // 如果希望,可以考虑每次新增,都能算出当前的最后一页自动跳转
        // 当前有多少页
        this.page.total++
        this.page.page = Math.ceil(this.page.total / this.page.pagesize)
      }
      // 3.提示
      this.$message.success('操作成功')
      // 4.刷新页面
      this.getRoleList()
      // 5.关闭弹窗
      this.showDialog = false
    },
    btnCancel() {
      // 因为编辑可能回显一些没有绑定过的数据,所以需要 手动清除
      this.roleForm = {
        name: '',
        description: ''
      }
      // 清理表单自带的清理函数
      // 可以清理表单绑定的数据 和 错误提示
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    async btnPermOK() {
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.permTree.getCheckedKeys()
      })
      this.$message.success('操作成功')
      this.btnPermCancel()
    },
    btnPermCancel() {
      this.$refs.permTree.setCheckedKeys([])
      this.showPermDialog = false
    },
    async assignPerm(id) {
      this.permList = listToTreeData(await getPermissionList(), '0')
      // console.log('00', this.permList)
      // 当前角色全选
      const { permIds } = await getRole(id)
      // console.log('11', permIds)
      // 点击记录id
      this.roleId = id
      this.showPermDialog = true
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    }
  }
}
</script>

<style>

</style>
